package com.itboye.ihomebank.adapter;

import java.util.ArrayList;
import java.util.List;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.HouseBean;
import com.itboye.ihomebank.bean.HouseListBean.HouseItem;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.FlowLayout;
import com.itboye.ihomebank.net.NetPublicParams;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

/**
 * 首页中显示的房屋列表
 */
public class HomeAdapter extends BaseAdapter {
	private Context context;

	private List<HouseItem> list;

	public HomeAdapter(Context context, List<HouseItem> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder = null;
		if (view == null) {
			holder = new ViewHolder();
			view = LayoutInflater.from(context).inflate(
					R.layout.item_home_adapter, null);
			holder.home_adapter = (ImageView) view
					.findViewById(R.id.home_adapter);
			holder.home_title = (TextView) view.findViewById(R.id.home_title);
			holder.home_xq = (TextView) view.findViewById(R.id.home_xq);
			holder.tag = (LinearLayout) view.findViewById(R.id.tag);
			holder.home_money = (TextView) view.findViewById(R.id.home_money);
			holder.home_danjia = (TextView) view.findViewById(R.id.home_danjia);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		XImageLoader.load(NetPublicConstant.IMAGE_URL
				+ list.get(position).getMainImg(), holder.home_adapter);
		holder.home_title.setText(list.get(position).getRentTyoe() + "·"
				+ list.get(position).getCommunity()
				+ list.get(position).getHouseRoom() + "室/"
				+ list.get(position).getHouseHall() + "厅/"
				+ list.get(position).getToilet() + "卫");
		holder.home_xq.setText(Html.fromHtml(list.get(position).getHouseArea()
				+ "㎡·" + list.get(position).getOritation() + "&nbsp;"
				+ "<font color='red' size='20'>" + Double.parseDouble(list.get(position).getRent())/100
				+ "元/月.</font>"));
//		holder.home_xq.setText(text)
		
		if (list.get(position).getEntrustMoney()!=null) {// 非经纪人
			holder.home_money.setText(Html.fromHtml(list.get(position).getArea() + "-"
					+ list.get(position).getAreaNone() + "&nbsp;"
					+ "<font color='red' size='20'>佣金"
					+ Double.parseDouble(list.get(position).getEntrustMoney())/100  + "元</font>"));
		}else{
			holder.home_money.setText(list.get(position).getArea() + "-"
					+ list.get(position).getAreaNone());
			// holder.home_danjia.setText(Double.parseDouble(list.get(position)
			// .getRent()) / 100 + "元/月");
		} 
		String str[] = null;
		holder.tag.removeAllViews();
		if (list.get(position).getTagNames() != null) {

			if (list.get(position).getTagNames().equals("")) {
				holder.tag.setVisibility(View.GONE);
			} else if (list.get(position).getTagNames().contains(",")) {
				str = list.get(position).getTagNames().split(",");
				for (int i = 0; i < str.length; i++) {
					View innerView = LayoutInflater.from(MyApplcation.ctx)
							.inflate(R.layout.item_biaoqian, null);
					TextView btn_name = (TextView) innerView
							.findViewById(R.id.btn_name);
					btn_name.setText(str[i]);
					LayoutParams layoutParams = new LayoutParams(
							LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT);
					layoutParams.setMargins(5, 5, 5, 5);
					holder.tag.addView(innerView, layoutParams);
				}

			} else {
				View innerView = LayoutInflater.from(MyApplcation.ctx).inflate(
						R.layout.item_biaoqian, null);
				TextView btn_name = (TextView) innerView
						.findViewById(R.id.btn_name);
				btn_name.setText(list.get(position).getTagNames());
				MarginLayoutParams layoutParams = new MarginLayoutParams(
						MarginLayoutParams.WRAP_CONTENT,
						MarginLayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(5, 5, 5, 5);
				holder.tag.addView(innerView, layoutParams);
			}
		}

		return view;
	}

	class ViewHolder {
		ImageView home_adapter;// 图片
		TextView home_title;// 标题
		TextView home_xq;// 详情
		TextView home_biaoqian_01, home_biaoqian_02, home_biaoqian_03;// 标签
		TextView home_money;// 价格
		TextView home_danjia;// 单价
		LinearLayout tag;
	}

}
